﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TPCN.DAL;
using TPCN.Common;
using TPCN.Commons;

namespace TPCN.Admincp
{
    public partial class KhachHang : System.Web.UI.Page
    {
        private static string KhachID = String.Empty;
        private static List<KhachHangObj> source;
        private static KhachHangObj obj;
        private static string oldPassword;

        private const string UPDATE = "Cập nhật";
        private const string INSERT = "Thêm";
        private const string CMD_EDIT = "cmdEdit";
        private const string CMD_DELETE = "cmdDelete";


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                BindGridView();
                InitForm();
            }
        }

        private void InitForm()
        {
            EditFormPanel.Visible = false;
            GridViewPanel.Visible = true;
            ButtonPanel.Visible = true;
        }

        private void BindGridView()
        {
            source = new List<KhachHangObj>();
            source = KhachHangDAL.GetByAll();
            KhachHangGridView.DataSource = source;
            KhachHangGridView.DataBind();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            TurnPanelOnOff();
            btnSave.Text = INSERT;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < KhachHangGridView.Rows.Count; i++)
            {
                GridViewRow row = KhachHangGridView.Rows[i];
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chk = (CheckBox)row.FindControl("chkSelect");
                    if (chk.Checked)
                    {
                        string ID = row.Cells[1].Text;
                        KhachHangDAL.Delete(ID);
                    }
                }
            }
            KhachHangGridView.PageIndex = 0;
            BindGridView();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (Page.IsValid)
            {
                SetValueFromForm();
                switch (btnSave.Text)
                {
                    case UPDATE:
                        KhachHangDAL.Update(obj);
                        BindGridView();
                        break;
                    case INSERT:
                        KhachHangDAL.Insert(obj);
                        BindGridView();
                        break;
                }
                ControlHelper.ResetControl(EditFormPanel);
                TurnPanelOnOff();
                obj = null;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            TurnPanelOnOff();
            ControlHelper.ResetControl(EditFormPanel);
        }

        protected void KhachHangGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            KhachID = e.CommandArgument.ToString();
            obj = new KhachHangObj();
            obj = KhachHangDAL.GetByID(KhachID);
            switch (e.CommandName)
            {
                case CMD_EDIT:
                    FillValueToForm(obj);
                    btnSave.Text = UPDATE;
                    TurnPanelOnOff();
                    break;
                case CMD_DELETE:
                    KhachHangDAL.Delete(KhachID);
                    BindGridView();
                    break;
            }
        }

        private void TurnPanelOnOff()
        {
            GridViewPanel.Visible = !GridViewPanel.Visible;
            EditFormPanel.Visible = !EditFormPanel.Visible;
            ButtonPanel.Visible = !ButtonPanel.Visible;
        }

        private void FillValueToForm(KhachHangObj obj)
        {
            txtHovaten.Text = String.IsNullOrEmpty(obj.Hovaten) ? String.Empty : obj.Hovaten;
            cboGioitinh.SelectedValue = String.IsNullOrEmpty(obj.Gioitinh.Trim()) || obj.Gioitinh == "0" ? "Nam" : obj.Gioitinh;
            txtSodienthoai.Text = String.IsNullOrEmpty(obj.Sodienthoai) ? String.Empty : obj.Sodienthoai;
            txtEmail.Text = obj.Email;
            txtPassword.Text = String.Empty;
            oldPassword = obj.Password;
            chkActive.Checked = Boolean.Parse(obj.Active);
        }

        protected void SetValueFromForm()
        {
            if (obj == null)
                obj = new KhachHangObj();
            obj.Hovaten = String.IsNullOrEmpty(txtHovaten.Text) ? String.Empty : txtHovaten.Text;
            obj.Gioitinh = cboGioitinh.SelectedValue;
            obj.Sodienthoai = txtSodienthoai.Text;
            obj.Email = txtEmail.Text;
            obj.Password = txtPassword.Text != String.Empty ? Security.EncryptPassword(txtPassword.Text) : oldPassword;
            obj.Active = Convert.ToString(chkActive.Checked);
        }

        protected void KhachHangGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            KhachHangGridView.PageIndex = e.NewPageIndex;
            BindGridView();
        }
    }
}